'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const { TEXT, DATE } = Sequelize;
    
    // 删除旧的字段
    await queryInterface.removeColumn('ui_configs', 'config_type');
    await queryInterface.removeColumn('ui_configs', 'config_key');
    await queryInterface.removeColumn('ui_configs', 'config_value');
    
    // 添加新的config字段
    await queryInterface.addColumn('ui_configs', 'config', {
      type: TEXT,
      allowNull: true,
      comment: '页面配置信息（JSON格式）'
    });
    
    // 更新created_at和updated_at字段
    await queryInterface.changeColumn('ui_configs', 'created_at', {
      type: DATE,
      allowNull: true,
      defaultValue: null
    });
    
    await queryInterface.changeColumn('ui_configs', 'updated_at', {
      type: DATE,
      allowNull: true,
      defaultValue: null
    });
  },

  down: async (queryInterface, Sequelize) => {
    const { STRING, TEXT, DATE } = Sequelize;
    
    // 删除新的config字段
    await queryInterface.removeColumn('ui_configs', 'config');
    
    // 恢复旧的字段
    await queryInterface.addColumn('ui_configs', 'config_type', {
      type: STRING(50),
      allowNull: false,
      comment: '配置类型: filter(筛选条件), column(列设置), pagination(分页信息)'
    });
    
    await queryInterface.addColumn('ui_configs', 'config_key', {
      type: STRING(100),
      allowNull: false,
      comment: '配置键名'
    });
    
    await queryInterface.addColumn('ui_configs', 'config_value', {
      type: TEXT,
      allowNull: true,
      comment: '配置值'
    });
    
    // 恢复created_at和updated_at字段
    await queryInterface.changeColumn('ui_configs', 'created_at', {
      type: DATE,
      allowNull: false,
      defaultValue: Sequelize.NOW
    });
    
    await queryInterface.changeColumn('ui_configs', 'updated_at', {
      type: DATE,
      allowNull: false,
      defaultValue: Sequelize.NOW
    });
  }
};